// ignore_for_file: use_build_context_synchronously, depend_on_referenced_packages

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/splash/splash_ad_view.dart';
import 'package:tiktokadult/hj_page/splash/splash_logic.dart';
import 'package:tiktokadult/tools_base/widget/LoadingWidget.dart';
import 'package:tiktokadult/hj_utils/screen.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import '../../config/config.dart';

class SplashPage extends StatefulWidget {
  static const routeName = '/splashPage';
  const SplashPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _SplashPageState();
  }
}

class _SplashPageState extends State<SplashPage> {
  SplashLogic logic = Get.put(SplashLogic());

  @override
  void initState() {
    super.initState();
    WakelockPlus.enable();
  }

  @override
  Widget build(BuildContext context) {
    screen.configData(context);
    return Scaffold(
      //这使用主要是预加载背景图
      body: GetBuilder<SplashLogic>(
        builder: (_) => Stack(
          fit: StackFit.expand,
          children: <Widget>[
            SizedBox(
              width: screen.screenWidth,
              height: screen.screenHeight,
              child: Image.asset(
                'assets/images/launch/ic_splash_bg.webp',
                fit: BoxFit.fill,
              ),
            ),
            if ((_.adLoadFinish || _.adsInfoBean != null))
              SplashAdView(
                adData: _.adsInfoBean,
                callback: () {
                  _.isJumpToHome = true;
                  _.jumpToHomeJudgeEvent();
                  setState(() {});
                },
              ),
            const Positioned(
              bottom: 20,
              right: 20,
              child: Text(
                "V${Config.innerVersion}",
                style: TextStyle(
                  color: Colors.red,
                  fontSize: 8,
                ),
              ),
            ),
            if (_.isJumpToHome) LoadingWidget(title: '数据初始化中...'),
          ],
        ),
      ),
    );
  }
}
